<?php

namespace App\Admin\Controllers\School;


use App\Admin\Extensions\CustomExporter;
use App\Admin\Extensions\Exporter\SchoolExporter;
use App\Models\School\User;
use App\Models\Sys\School;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Request;

class IntentionController extends Controller
{
    use ModelForm;

    public $header = '意向学校';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {

            $grid->model()->where('status', 2)->where('role_id', -1)->where('school_id', null)->orderBy('id','desc');
            $grid->id('ID');
            $grid->mobile('手机号');
            $grid->create_time('创建时间');

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
                $actions->disableEdit();
                $actions->prepend('<a href="/school/intention/'.$actions->row->id.'/edit" class="grid-row-edit"><i class="fa fa-edit"></i>授权</a>');
            });

            $grid->disableCreateButton();

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('mobile', '手机号');
            });

            //导出
            $grid->exporter(new SchoolExporter($grid, $this->header));
            /*$head = ['手机号', '创建时间'];
            $body = ['mobile', 'create_time'];
            $grid->exporter(new CustomExporter($grid, $this->header, $head, $body));*/

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->display('mobile', '手机号');

            $form->select('school_id', '学校')->options(
                School::leftJoin('school_user as b', 'b.id', 'jxt_sys_school.id')
                    ->where('b.id', null)
                    ->pluck('jxt_sys_school.school_name', 'jxt_sys_school.id')
            );

            $form->display('create_time', '创建时间');

            $form->saving(function (Form $form){
                $form->model()->status = 1;
                $form->model()->role_id = 1;
            });
        });
    }



}
